//files
AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua")

//max/hits
ENT.Hits = 0;
ENT.MaxHits = 12;

//sounds
ENT.Sounds =
{
	StickBody = Sound("weapons/scissor/bodystick.wav"),
	StickWall = Sound("weapons/scissor/wallstick.wav"),
	Bounce = Sound("weapons/scissor/bounce.wav")
}

//npcs that aren't made of flesh
ENT.MetalicNPCs =
{
	"npc_manhack",
	"npc_cscanner",
	"npc_dog",
	"npc_combinedropship",
	"npc_combinegunship",
	"npc_turret_ceiling",
	"npc_turret_floor",
	"npc_rollermine",
	"npc_helicopter",
	"npc_strider"
}

//when initializing
function ENT:Initialize()

	//info
	self.Entity:SetModel("models/weapons/w_hasami.mdl");
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);

	//physics object
	local Physics = self.Entity:GetPhysicsObject();

	//if valid
	if ValidEntity(Physics) then

		//wake, disable gravity
		Physics:Wake();
		Physics:EnableGravity(false);

		//shoot it
		Physics:ApplyForceCenter(self.Entity:GetAngles():Forward() * 4000);

	end

	//and trail
	self.Trail = util.SpriteTrail(self.Entity,0,Color(213,255,255,225),false,12,2.5,0.4,1 / (14.5 / 2),"trails/whiteelec.vmt");

end

//when colliding
function ENT:PhysicsCollide(Data,Physics)

	//get entity
	local Ent = Data.HitEntity;

	//do we want to remove?
	local Rem = false;

	//if exceeded max hits
	if self.Hits >= self.MaxHits then

		//if hit npc
		if Ent:IsNPC() || Ent:IsPlayer() then

			//play this sound
			self.Entity:EmitSound(self.Sounds.StickBody);

		else

			//else, play that sound
			self.Entity:EmitSound(self.Sounds.StickWall);

		end

		//remove it
		Rem = true;

	else

		//if hit npc/player
		if Ent:IsNPC() || Ent:IsPlayer() then

			//sound
			self.Entity:EmitSound(self.Sounds.StickBody);

			//remove
			Rem = true;

		else

			//else, play this one
			self.Entity:EmitSound(self.Sounds.Bounce);

		end

		//increment hits
		self.Hits = self.Hits + 1;

	end

	//direction, hit normal
	local Dir = Data.OurOldVelocity:GetNormal();
	local Norm = Data.HitNormal;

	//reflect the direction
	local TargetDir = Dir - 2 * (Norm:DotProduct( Dir ) * Norm);

	//set angles
	self.Entity:SetAngles(TargetDir:Angle());

	//stop it from moving - crap this sucks
	Physics:Sleep();
	Physics:Wake();

	//shoot it
	Physics:ApplyForceCenter(TargetDir * 4000);

	//hit entity takes damage
	Ent:TakeDamage(80 + math.Rand(5,15),self.Entity:GetOwner(),self.Entity);

	//some stuff for the effect
	local M = 4;

	//if last hit
	if Rem then

		//awesomer sparks
		M = 5;

	end

	//effect data
	local FX = EffectData();
	FX:SetOrigin(Data.HitPos);
	FX:SetNormal(Data.HitNormal);
	FX:SetMagnitude(1.8);
	FX:SetScale(M);
	FX:SetRadius(5.5);

	//spark effect
	util.Effect("sparks",FX,true,true);

	//decal
	util.Decal("ManhackCut",Data.HitPos - Data.HitNormal,Data.HitPos + Data.HitNormal);

	//if we hit a player/npc and it's made from flesh
	if Ent:IsPlayer() || (Ent:IsNPC() && !table.HasValue(self.MetalicNPCs,Ent:GetClass())) then

		//3 times
		for I = 1,3 do

			//blood effect
			local FX = EffectData();
			FX:SetOrigin(Data.HitPos + (Data.HitNormal * I / 2));
			util.Effect("BloodImpact",FX,true,true);

		end

	end

	//if removing
	if Rem then

		//remove us
		self.Entity:Remove();

	end

end